package map.android.baidu.carpool.utils;

import map.android.baidu.carpool.R;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @Author chenlei18
 * @Description 公共title
 * @CreateTime 2015-3-27 下午4:03:04
 */
public class TitleHelpler {

    private View mView;
    private View mLeftView;
    private TextView mCenterTextView;
    private TextView mRightTextView;
    private View mRightImageView;

    public TitleHelpler(View view) {
        mView = view;
        initTitleView();
    }

    private void initTitleView() {
        mLeftView = mView.findViewById(R.id.left_view);
        mCenterTextView = (TextView) mView.findViewById(R.id.center_view);
        mRightTextView = (TextView) mView.findViewById(R.id.right_view);
        mRightImageView = mView.findViewById(R.id.right_img);
    }

    public void setLeftViewOnClickListener(OnClickListener listener) {
        mLeftView.setOnClickListener(listener);
    }

    public void setRightViewText(String content) {
        setRightViewTextShow();
        if (content != null && !content.trim().equals(""))
            mRightTextView.setText(content);

    }

    /**
     * 只显示右边的textview
     */
    public void setRightViewTextShow() {
        mRightTextView.setVisibility(View.VISIBLE);
        mRightImageView.setVisibility(View.GONE);
    }

    /**
     * 只显示右边的imageview
     */
    public void setRightViewImageShow() {
        mRightTextView.setVisibility(View.GONE);
        mRightImageView.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏右边的view
     */
    public void setRightViewHide() {
        mRightTextView.setVisibility(View.GONE);
        mRightImageView.setVisibility(View.GONE);
    }

    public void setRightViewOnClickListener(OnClickListener listener) {

        if (mRightImageView.getVisibility() == View.VISIBLE)
            mRightImageView.setOnClickListener(listener);
        else
            mRightTextView.setOnClickListener(listener);
    }

    public void setCenterViewText(String content) {

        if (!TextUtils.isEmpty(content)) {
            mCenterTextView.setText(content);
            mCenterTextView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * back按钮
     * 
     * @param leftlistener
     */
    public void setLeftView(OnClickListener leftlistener) {
        setLeftViewOnClickListener(leftlistener);
        mLeftView.setVisibility(View.VISIBLE);
    }

    /**
     * @param centerText 中间显示的文字
     */
    public void setCenterView(String centerText) {
        setCenterViewText(centerText);
    }

    /**
     * 右边显示图片
     * 
     * @param rightlistener 右边的事件监听
     */
    public void setRightView(OnClickListener rightlistener) {
        setRightViewImageShow();
        setRightViewOnClickListener(rightlistener);
    }

    /**
     * 右边显示图片 提供图片地址
     * 
     * @param resId 图片的res R.drawable.xxx
     * @param rightlistener 右边的事件监听
     */
    public void setRightView(int resId, OnClickListener rightlistener) {
        ((ImageView) mRightImageView).setImageResource(resId);
        setRightViewImageShow();
        setRightViewOnClickListener(rightlistener);
    }

    /**
     * @param resId
     */
    public void setRightViewImageResource(int resId) {
        ((ImageView) mRightImageView).setImageResource(resId);
    }

    /**
     * 右边显示文字
     * 
     * @param rightText 右边显示的文字
     * @param rightlistener 右边的事件监听
     */
    public void setRightView(String rightText, OnClickListener rightlistener) {
        setRightViewText(rightText);
        setRightViewOnClickListener(rightlistener);
    }

    public void setRightViewTextColor(int color) {
        mRightTextView.setTextColor(color);
    }

}
